#include <linux/init.h>
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/device.h>
#include <asm/uaccess.h>

//物理地址
#define GPX2_CON 0x11000C40 
#define GPX2_SIZE 8

//存放虚拟地址的指针
volatile unsigned long *gpx2conf;
volatile unsigned long *gpx2dat;

static unsigned int dev_major =250;  //主设备号
struct class *devcls;
struct device * dev;
static int kernel_val =555;

//打开
int char_drv_open (struct inode *inode, struct file *filp)
{
	printk("--------%s----------\n", __FUNCTION__);
	return 0;
}

//读
ssize_t char_drv_read (struct file *filp, char __user *buf, size_t count, loff_t *fpos)
{
	int ret;

	printk("--------%s----------\n", __FUNCTION__);
	
	ret = copy_to_user(buf, &kernel_val, count);
	if(ret > 0)
	{
		printk("copy_to_user error\n");
		return -EFAULT;
	}
		
	return 0;
}

//写
ssize_t char_drv_write (struct file *filp, const char __user *buf, size_t count, loff_t *fpos)
{
	int ret;
	
	printk("--------%s----------\n", __FUNCTION__);

	ret = __copy_from_user(&kernel_val, buf, count);
	if(ret > 0)
	{
		printk("copy_from_user error\n");
		return -EFAULT;
	}
	printk("--------kernel------value=%d----\n", kernel_val);
	
	return 0;
}

//关闭
int char_drv_close (struct inode *inode, struct file *filp)
{
	printk("--------%s----------\n", __FUNCTION__);
	return 0;
}


const struct file_operations my_fops = {
	.open = char_drv_open,
	.read = char_drv_read,
	.write = char_drv_write,
	.release = char_drv_close
};

static int __init hello_drv_init(void)
{
	
	//申请设备号
	int ret;
	ret = register_chrdev(dev_major, "chr_dev_test", &my_fops);
	if(ret == 0)
	{
		printk("register ok\n");
	} else {
		printk("register failed\n");
	}

	//创建设备节点
	devcls = class_create(THIS_MODULE, "chr_cls");
	dev = device_create(devcls, NULL, MKDEV(dev_major, 0), NULL, "chr2");
	
	
	return 0;
}

static void __exit hello_drv_exit(void)
{
	device_destroy(devcls, MKDEV(dev_major, 0));
	class_destroy(devcls);
	unregister_chrdev(dev_major, "chr_dev_unregister");
}


module_init(hello_drv_init);
module_exit(hello_drv_exit);
MODULE_LICENSE("GPL");


	
